$computer = "172.16.27.183"
$command = "echo 'eeor............' "


function authcation {    
    Write-Output "start authcation"
	$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
	$headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
	$headers.Add("Accept-Language", "zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7")
	$headers.Add("Cache-Control", "max-age=0")
	$headers.Add("Content-Type", "application/x-www-form-urlencoded")
	$headers.Add("Cookie", "sessionid=34e3dabf5fac2be1beb04d2cd5b365d39f26089890c0b69c2d305f0bc8b5028b")
	$headers.Add("Origin", "https://10.1.1.1:20000")
	$headers.Add("Referer", "https://10.1.1.1:20000/cn/login.html")
	$headers.Add("Sec-Fetch-Dest", "document")
	$headers.Add("Sec-Fetch-Mode", "navigate")
	$headers.Add("Sec-Fetch-Site", "same-origin")
	$headers.Add("Sec-Fetch-User", "?1")
	$headers.Add("Upgrade-Insecure-Requests", "1")
	$headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
	$headers.Add("sec-ch-ua", "`"Not/A)Brand`";v=`"99`", `"Google Chrome`";v=`"115`", `"Chromium`";v=`"115`"")
	$headers.Add("sec-ch-ua-mobile", "?0")
	$headers.Add("sec-ch-ua-platform", "`"Windows`"")

	$body = "username=18267199586&password=Citydo@123&RedirectUrl=&anonymous=DISABLE&anonymousurl=&accesscode=&accesscode1=DISABLE&checkbox=on&checkbox1=on"

	$response = Invoke-WebRequest -Uri 'https://10.1.1.1:20000/cn/login' -Method 'POST' -SkipCertificateCheck -Headers $headers -Body $body 
	$response | ConvertTo-Json
}  



while ($true) {

	$maxAttempts = 4
	$failedAttempts = 0

	for ($i = 1; $i -le $maxAttempts; $i++) {
		$result = Test-Connection -ComputerName $computer -Count 2 -Delay 1 -Quiet -TimeoutSeconds 2
		if (!$result) {
			$failedAttempts++
			Write-Host "that $i times fail：$computer"
		} else {
			$failedAttempts = 0
			break
		}
	}
	$currentDateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
	if ($failedAttempts -eq $maxAttempts) {
		Write-Output " *************************************** $currentDateTime ********************* Ping fail: disconnect $computer."
		authcation
	} else {
		Write-Output "$currentDateTime Ping success: $computer connedted."
	}

    # 设置Ping的间隔时间（以毫秒为单位）
    Start-Sleep -Milliseconds 5000
}


